package com.tz.cartelo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.tz.cartelo.R;
import com.tz.cartelo.model.GoodsCategory;
import com.tz.cartelo.model.GoodsList;
import com.tz.cartelo.util.CommonUtils;

import java.util.List;


/**
 * Created by ok on 2016/9/18.
 */
public class GoodsClassifyGridAdapter extends BaseAdapter {
    private LayoutInflater mLayoutInflater;
    private List<GoodsCategory> mGoodsCategoryList;
    private Context mContext;
    private int mImgWidth, mImgHeight;

    public GoodsClassifyGridAdapter(Context context, List<GoodsCategory> goodsCategoryList) {
        mLayoutInflater = LayoutInflater.from(context);
        mContext = context;
        mGoodsCategoryList = goodsCategoryList;
        setImgSize();
    }

    private void setImgSize() {
        mImgWidth = (CommonUtils.getScreenWidth(mContext) - 5 * mContext.getResources().getDimensionPixelOffset(R.dimen.margin_10) ) / 4;
        mImgHeight = mImgWidth ;
        //+ mContext.getResources().getDimensionPixelOffset(R.dimen.margin_5)
    }

    @Override
    public int getCount() {
        return mGoodsCategoryList.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = mLayoutInflater.inflate(R.layout.item_goods_classify_grid, null);
            holder = new ViewHolder();
            holder.goodsClassifyGridImg = (ImageView) view.findViewById(R.id.goods_classify_grid_img);
            holder.goodsClassifyGridText = (TextView) view.findViewById(R.id.goods_classify_grid_text);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(mImgWidth, mImgHeight);
        holder.goodsClassifyGridImg.setLayoutParams(layoutParams);
//        ImageLoader.getInstance().displayImage("drawable://" + R.mipmap.ic_holder_list, holder.itemGoodsDetailsImg, CommonUtils.optionsList());
        ImageLoader.getInstance().displayImage(mGoodsCategoryList.get(i).getGoodsPicture(), holder.goodsClassifyGridImg, CommonUtils.optionsList());
        holder.goodsClassifyGridText.setText(mGoodsCategoryList.get(i).getTitle());
        return view;
    }

    private class ViewHolder {
        ImageView goodsClassifyGridImg;
        TextView goodsClassifyGridText;
    }

}
